package com.raverun.imovies.jadeditor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;

public class SelectJadFileAction extends Action implements ActionFactory.IWorkbenchAction
{
    public SelectJadFileAction( IWorkbenchWindow window )
    {
        mWindow = window;
        setId( ID );
        setText( "Open ..." );
    }

    public void dispose()
    {
    }
    
    @Override
    public void run()
    {
        showFileDialog();
    }

    private void showFileDialog()
    {
        FileDialog d = new FileDialog( mWindow.getShell(), SWT.OPEN );
        d.setText( "Select a JAD file" );
        d.setFilterNames( new String[] { "j2me jad files" } );
        d.setFilterExtensions( new String[] { "*.jad" } );
        d.setFilterPath( "/home/gavin/Desktop/plugins" );

        String selected = d.open();
        if ( selected == null )
        {
            return;
        }
        
        System.out.println( "File " + selected );
        
        openFile( selected );     
    }    
    
    private void openFile( String selected )
    {
        IWorkspace ws = ResourcesPlugin.getWorkspace();
        IProject project = ws.getRoot().getProject("External Files");
        
        try
        {
            if (!project.exists())
               project.create(null);
            if (!project.isOpen())
               project.open(null);

            Shell shell = mWindow.getShell();
            if (selected == null)
               return;
            IPath location = new Path(selected);
            IFile file = project.getFile(location.lastSegment());
            file.createLink(location, IResource.NONE, null);
            IWorkbenchPage page = mWindow.getActivePage();            

            IPathEditorInput input = new MyFileEditorInput( file );
            page.openEditor( input, "org.eclipse.ui.DefaultTextEditor" );            
        }
        catch( CoreException ce )
        {
            System.out.println( ce.getMessage() );
        }
        
//        FileEditorInput input = new FileEditorInput();
//        try {
//          page.openEditor(input, ChatEditor.ID);
//        } catch (PartInitException e) {
//          // handle error
//        }        
//        
//        
//        
//        if (page != null)
//           page.openEditor( input, editorId );    
    }

    public final static String ID = "com.raverun.imovies.jadeditor.selectjadfile"; //$NON-NLS-1$    
    
    private final IWorkbenchWindow mWindow;
}
